package com.cw.jeeyt.service.interceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import com.cw.jeeyt.common.utils.DateUtils;
import com.cw.jeeyt.service.bo.security.SecurityUserBo;
import com.cw.jeeyt.core.util.SessionUtil;

public class UserValidatorInterceptor implements HandlerInterceptor {

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object object,
        Exception exception) throws Exception {

    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object object, ModelAndView mv)
    throws Exception {
    }

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object arg2) throws Exception {
        SecurityUserBo user = SessionUtil.getSessionUser();
        if (user == null || "anonymousUser".equals(user.getUsername())) {
            response.sendRedirect(request.getContextPath() + "/login/to-login?timestamp=" + DateUtils.getDate("yyyyMMddhhmmss"));
            return false;
        }
        return true;
    }

}
